package cn.operation.backend.service;

import cn.operation.backend.enums.DeleteStatusEnum;
import cn.operation.base.mapper.ItfLatestCodePondMapper;
import cn.operation.base.model.ItfLatestCodePond;
import cn.operation.base.model.ItfLatestCodePondExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ItfLatestCodePondServiceImpl implements ItfLatestCodePondService {
    private static final Logger LOGGER = LoggerFactory.getLogger(WayBillServiceImpl.class);
    @Autowired
    private ItfLatestCodePondMapper itfLatestCodePondMapper;

    @Override
    public List<ItfLatestCodePond> queryCodes(String type) throws Exception {
        if (type == null) {
            LOGGER.error("WayBillServiceImpl.queryCodes param type must not be null ");
            throw new IllegalArgumentException("状态不允许为空");
        }
        List<ItfLatestCodePond> list = new ArrayList<ItfLatestCodePond>();
        try {
            ItfLatestCodePondExample example = new ItfLatestCodePondExample();
            example.createCriteria().andTypeEqualTo(type).andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue());
            list = itfLatestCodePondMapper.selectByExample(example);

        } catch (Exception e) {
            LOGGER.error("WayBillServiceImpl.queryCodes ： query codes fail ");
            throw new IllegalArgumentException("查询单据编码失败:"+ e);
        }
//        if (list.size()>0){
//            try {
//                list.stream().forEach(i ->{
//                    i.setStatus(DeleteStatusEnum.DELETRE.getValue());
//                });
//                list.stream().forEach(i ->{
//                    itfLatestCodePondMapper.updateByPrimaryKeySelective(i);
//                });
//            } catch (Exception e) {
//                LOGGER.error("WayBillServiceImpl.queryCodes ： update delete fail ");
//                throw new IllegalArgumentException("修改单据编码删除状态失败:"+ e);
//            }
//        }
        return list;
    }

    @Override
    public Integer addCode(String code, String type) throws Exception {
        if (code == null) {
            LOGGER.error("WayBillServiceImpl.addCode param code must not be null ");
            throw new IllegalArgumentException("单据号不允许为空");
        }
        if (type == null) {
            LOGGER.error("WayBillServiceImpl.addCode param type must not be null ");
            throw new IllegalArgumentException("单据类型不允许为空");
        }
        ItfLatestCodePondExample example = new ItfLatestCodePondExample();
        example.createCriteria().andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue()).andCodeEqualTo(code);
        List<ItfLatestCodePond> list = itfLatestCodePondMapper.selectByExample(example);
        ItfLatestCodePond codePond = new ItfLatestCodePond();
        if (list.size() > 0){
            LOGGER.info("WayBillServiceImpl.addCode code already exist ");
        }else {
            codePond.setCode(code);
            codePond.setType(type);
            codePond.setStatus(DeleteStatusEnum.NORMAL.getValue());
            itfLatestCodePondMapper.insertSelective(codePond);
        }
        return null;
    }
}
